//
//  FollowTheCircleGameScene.cpp
//  CanYouDoIt
//
//  Created by hoangtx on 7/10/13.
//
//

#include "FollowTheCircleGameScene.h"
#include "GameLayerManager.h"
#include "SceneManager.h"
#include "MyUtilities.h"

CCScene* FollowTheCircleGameScene::scene(){
    // 'scene' is an autorelease object
    CCScene *scene = CCScene::create();
    
    // 'layer' is an autorelease object
    FollowTheCircleGameScene *layer = FollowTheCircleGameScene::create();
    GameLayerManager::sharedInstance()->followTheCircleGameLayer = layer;
    
    // add layer as a child to scene
    scene->addChild(layer);
    
    // return the scene
    return scene;
}


bool FollowTheCircleGameScene::init(){
    CountTimeGameScene::init();
    CCSize winSize = CCDirector::sharedDirector()->getWinSize();
    double buttonSize = winSize.width*0.30f;
    double radius = (((winSize.height-scoreAndTimeLayer->getBarHeight())/2)*0.75f)/2;
    double offsetTop = (winSize.height-scoreAndTimeLayer->getBarHeight())/2 - 2*radius;
    circleButton = MyCircleEightButton::create("circle_button.png", (winSize.width-buttonSize)/2, (winSize.height-scoreAndTimeLayer->getBarHeight())/2-buttonSize/2, buttonSize, buttonSize);
    CCCallFuncO* callback = CCCallFuncO::create(this, callfuncO_selector(FollowTheCircleGameScene::onTapCircleButton), circleButton);
    circleButton->addCallback(callback);
    
    CCPoint originPoint = ccp(circleButton->getPositionX(),circleButton->getPositionY()+radius);
    circleButton->setCircle(originPoint, radius);
    this->addChild(circleButton);
    circleButton->randomDirection();
    return true;
}

void FollowTheCircleGameScene::onTapCircleButton(cocos2d::CCObject *sender){
    gameOver();
}
void FollowTheCircleGameScene::onTapEightButton(cocos2d::CCObject *sender){
    gameOver();
}

void FollowTheCircleGameScene::update(float dt){
    circleButton->handleUpdate(dt);
}

void FollowTheCircleGameScene::startGame(){
    setTouchEnabled(true);
    scoreAndTimeLayer->setVisible(true);
    SceneManager::sharedInstance()->previousScene = SceneManager::sharedInstance()->followTheCircleGameScene;
    //this->scheduleUpdate();
}

void FollowTheCircleGameScene::reset(){
    CountTimeGameScene::reset();
    circleButton->resetState();
}

void FollowTheCircleGameScene::onTick(float dt){
    CountTimeGameScene::onTick(dt);
    if(circleButton->isAtMaxSpeed()==false){
        if(numberOfSecondsPassed%2==0){
            circleButton->increseVelocityBy(M_PI/450);
        }
        
    }else{
        if(numberOfSecondsPassed%4==0){
            circleButton->increseVelocityBy(M_PI/450);
        }
    }
    
}

void FollowTheCircleGameScene::draw(){
    CountTimeGameScene::draw();
    CCPoint realTopOrigin = ccp(circleButton->topOrigin.x+circleButton->getBoundingRect().size.width/2,circleButton->topOrigin.y+circleButton->getBoundingRect().size.height/2);
    CCPoint realBotOrigin = ccp(circleButton->botOrigin.x+circleButton->getBoundingRect().size.width/2,circleButton->botOrigin.y+circleButton->getBoundingRect().size.height/2);
    ccDrawCircle(realTopOrigin, circleButton->radius, 0, 120, false);
    ccDrawCircle(realBotOrigin, circleButton->radius, 0, 120, false);
}

void FollowTheCircleGameScene::ccTouchesBegan(cocos2d::CCSet *touches, cocos2d::CCEvent *pEvent){
    circleButton->handleTouchesBegan(touches, pEvent);
    if(circleButton->isFocused){
        this->schedule(schedule_selector(CountTimeGameScene::onTick), 1);
        this->scheduleUpdate();
    }
}

void FollowTheCircleGameScene::ccTouchesMoved(cocos2d::CCSet *touches, cocos2d::CCEvent *pEvent){
    circleButton->handleTouchesMoved(touches, pEvent);
    if(circleButton->isFocused==false){
        gameOver();
    }
}

void FollowTheCircleGameScene::ccTouchesEnded(cocos2d::CCSet *touches, cocos2d::CCEvent *pEvent){
    circleButton->handleTouchesEnded(touches, pEvent);
}

